<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类参数</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input clearable
                    @clear="getGoodsList"
                    placeholder="请输入内容"
                    v-model="queryData.query"
                    class="input-with-select">
            <el-button @click="getGoodsList"
                       slot="append"
                       icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button @click="$router.push('/goods/add')"
                     type="primary">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table border
                stripe
                :data="goods">
        <el-table-column type="index"
                         label="#"></el-table-column>
        <el-table-column prop="goods_name"
                         label="商品名称"></el-table-column>
        <el-table-column width="110"
                         prop="goods_price"
                         label="商品价格(元)"></el-table-column>
        <el-table-column width="110"
                         prop="goods_weight"
                         label="商品重量"></el-table-column>
        <el-table-column width="140"
                         label="创建时间">
          <template slot-scope="scope">
            {{scope.row.upd_time |formatDate}}
          </template>
        </el-table-column>
        <el-table-column width="140"
                         label="操作">
          <template slot-scope="scope">
            <el-button icon="el-icon-edit"
                       type="primary"
                       size="mini"></el-button>
            <el-button @click="removeGoods(scope.row.goods_id)"
                       icon="el-icon-delete"
                       type="danger"
                       size="mini"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     background
                     :current-page="queryData.pagenum"
                     :page-sizes="[5,10,20,30]"
                     :page-size="queryData.pagesize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryData: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      goods: [],
      total: 0
    }
  },

  created () {
    this.getGoodsList()
  },

  methods: {
    async getGoodsList () {
      const { data: res } = await this.$axios.get('goods', {
        params: this.queryData
      })
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      this.goods = res.data.goods
      this.total = res.data.total
    },
    handleSizeChange (val) {
      this.queryData.pagesize = val
      this.getGoodsList()
    },
    handleCurrentChange (val) {
      this.queryData.pagenum = val
      this.getGoodsList()
    },
    removeGoods (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async _ => {
        const { data: res } = await this.$axios.delete('goods/' + id)
        if (res.meta.status !== 200) return this.$message.error('删除失败!')
        this.$message.success('删除成功')
        this.getGoodsList()
      }).catch(() => {
        this.$message('已取消删除')
      })
    }
  }
}
</script>

<style scoped lang='less'>
</style>
